import time

import pytest
from appium import webdriver
from appium.webdriver.common.appiumby import AppiumBy
from app_setup import app_init_setting
from appium.options.android import UiAutomator2Options


def login():
    print('开始执行用例...')
    appium_host_url = 'http://127.0.0.1:4723/wd/hub'
    end_user_desire_caps_json = app_init_setting('end_user')
    # print('desire_caps: '+desire_caps_json)
    if end_user_desire_caps_json is not None:
        options = UiAutomator2Options().load_capabilities(end_user_desire_caps_json) #这里注意UiAutomator2Options后面一定要加上()
        driver = webdriver.Remote(appium_host_url, options=options)
        return driver

    else:
        raise ValueError('desire_caps must not be None')
        return None



def test_login():
    driver = login()
    time.sleep(5)
    contextvars = driver.page_source
    print(contextvars)
    clear_click = driver.find_element(AppiumBy.XPATH,

                                      '/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.view.ViewGroup/android.widget.FrameLayout/android.widget.LinearLayout/android.webkit.WebView/android.webkit.WebView/android.view.View/android.view.View[2]/android.view.View/android.view.View/android.widget.TextView')
    clear_click.click()
    user_name_input = driver.find_element(AppiumBy.XPATH,
                                          '/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.view.ViewGroup/android.widget.FrameLayout/android.widget.LinearLayout/android.webkit.WebView/android.webkit.WebView/android.view.View/android.view.View[2]/android.view.View/android.view.View/android.view.View/android.view.View/android.widget.EditText')
    user_name_input.send_keys('15158192691@gmail.com')
    passwd_box = driver.find_element(AppiumBy.XPATH,
                                     '/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.view.ViewGroup/android.widget.FrameLayout/android.widget.LinearLayout/android.webkit.WebView/android.webkit.WebView/android.view.View/android.view.View[3]/android.view.View/android.view.View/android.view.View/android.view.View/android.widget.EditText')
    passwd_box.send_keys('wangqz617')
    signUp_button = driver.find_element(AppiumBy.XPATH,
                                        '/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.view.ViewGroup/android.widget.FrameLayout/android.widget.LinearLayout/android.webkit.WebView/android.webkit.WebView/android.view.View/android.widget.TextView[2]')
    signUp_button.click()

